//
//  FNBannerCollectionViewFlowLayout1.swift
//  FNBanner
//
//  Created by Fnoz on 2016/10/29.
//  Copyright © 2016年 Fnoz. All rights reserved.
//

import UIKit

import UIKit

class FNBannerCollectionViewFlowLayout1: UICollectionViewFlowLayout {
    //两边的元素是否缩放和降低透明度
    var scaleItems: Bool = true
    
    //scaleOffset外开始scale & alpha
    var scaleOffset = UIScreen.main.bounds.width * 0.6
    
    //最低缩放比例
    var minimumScaleFactor: CGFloat = 0.75
    
    //最低透明度
    var minimumAlphaFactor: CGFloat = 0.75
    
    fileprivate var lastCollectionViewSize: CGSize = CGSize.zero
    
    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }
    
    init(itemSize: CGSize) {
        super.init()
        scrollDirection = .horizontal
    }
}

extension FNBannerCollectionViewFlowLayout1 {
    
    override func invalidateLayout(with context: UICollectionViewLayoutInvalidationContext) {
        super.invalidateLayout(with: context)
        
        guard let collectionView = self.collectionView else { return }
        
        if collectionView.bounds.size != lastCollectionViewSize {
            let inset = collectionView.bounds.size.width / 2 - itemSize.width / 2
            collectionView.contentInset  = UIEdgeInsetsMake(0, inset, 0, inset)
            collectionView.contentOffset = CGPoint(x: -inset, y: 0)
            self.lastCollectionViewSize = collectionView.bounds.size
        }
    }
    
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        guard let collectionView = self.collectionView else {
            return proposedContentOffset
        }
        
        let proposedRect = CGRect(x: proposedContentOffset.x, y: 0, width: collectionView.bounds.width, height: collectionView.bounds.height)
        guard let layoutAttributes = self.layoutAttributesForElements(in: proposedRect) else {
            return proposedContentOffset
        }
        
        var candidateAttributes: UICollectionViewLayoutAttributes?
        let proposedContentOffsetCenterX = proposedContentOffset.x + collectionView.bounds.width / 2
        
        for attributes in layoutAttributes {
            if attributes.representedElementCategory != .cell {
                continue
            }
            
            if candidateAttributes == nil {
                candidateAttributes = attributes
                continue
            }
            
            if fabs(attributes.center.x - proposedContentOffsetCenterX) < fabs(candidateAttributes!.center.x - proposedContentOffsetCenterX) {
                candidateAttributes = attributes
            }
        }
        
        guard let aCandidateAttributes = candidateAttributes else {
            return proposedContentOffset
        }
        
        var newOffsetX = aCandidateAttributes.center.x - collectionView.bounds.size.width / 2
        let offset = newOffsetX - collectionView.contentOffset.x
        
        if (velocity.x < 0 && offset > 0) || (velocity.x > 0 && offset < 0) {
            let pageWidth = self.itemSize.width + self.minimumLineSpacing
            newOffsetX += velocity.x > 0 ? pageWidth : -pageWidth
        }
        
        return CGPoint(x: newOffsetX, y: proposedContentOffset.y)
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        guard let _ = self.collectionView,
            let superAttributes = super.layoutAttributesForElements(in: rect) else {
                return super.layoutAttributesForElements(in: rect)
        }
        if scaleItems == false{
            return super.layoutAttributesForElements(in: rect)
        }
        
        guard case let newAttributesArray as [UICollectionViewLayoutAttributes] = NSArray(array: superAttributes, copyItems: true) else {
            return nil
        }
        
        newAttributesArray.forEach {
            $0.transform = CGAffineTransform.init(scaleX: incline, y: incline)
        }
        
        return newAttributesArray
    }
}
